# Change the target if you need (probably flags have to be changed also)
PREFIX="aarch64"
#PREFIX="arm"
TARGET="${PREFIX}-linux-ohos"
#Uncomment to select arm
#EXTRA_FLAGS="-march=armv7-a"

# Fill in these 4 variables with your paths
WORK_DIR=${PWD}
LLVM_PROJECT_DIR="${WORK_DIR}/llvm-project"
LLVM_TABLEGEN_BINARY="${WORK_DIR}/llvm-tblgen"
CLANG_PATH=$(find "${WORK_DIR}" -maxdepth 1 -type d -name 'clang-*')
SYSROOT="${WORK_DIR}/sysroot"

TOOLCHAIN_BIN_PATH="${CLANG_PATH}/bin"
FLAGS="--target=${TARGET} --sysroot ${SYSROOT} ${EXTRA_FLAGS} -L${CLANG_PATH}/lib/${TARGET}/c++ -B${CLANG_PATH}/lib/${TARGET}/c++ -static -v"


pushd "${PWD}/llvm-project/llvm-build"
cmake -G Ninja \
      "${LLVM_PROJECT_DIR}/llvm"
ninja install-llvm-tblgen
cp -a ./bin/llvm-tblgen ${WORK_DIR}
popd

cmake -G Ninja \
    -DCMAKE_CROSSCOMPILING=True \
    -DCMAKE_BUILD_TYPE=Release \
    -DLLVM_DEFAULT_TARGET_TRIPLE="${TARGET}" \
    -DCMAKE_INSTALL_PREFIX="${PWD}/install" \
    -DLLVM_TABLEGEN="${LLVM_TABLEGEN_BINARY}" \
    -DCMAKE_C_FLAGS="${FLAGS}" \
    -DCMAKE_CXX_FLAGS="${FLAGS}" \
    -DCMAKE_C_COMPILER="${TOOLCHAIN_BIN_PATH}/clang" \
    -DCMAKE_CXX_COMPILER="${TOOLCHAIN_BIN_PATH}/clang++" \
    "${LLVM_PROJECT_DIR}/llvm"

ninja -j8 install-llvm-symbolizer
mv ./install/bin/llvm-symbolizer ./llvm-symbolizer-${PREFIX}